<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/conf/global.config.php');
require_once(SRC_DIR.'util.php');

define('LOG_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))); 
function Logs($str){
		file_put_contents(LOG_PATH. "/log.txt", "[". date("Y-m-d H:i:s") . "]". $str ."\r\n" ,  FILE_APPEND );
}

if ($_SERVER['REQUEST_METHOD'] == 'PUT')
{
	$filename = explode('/', $_SERVER["REQUEST_URI"])[4];
	Logs("------save-----");
	Logs($filename);
	if (strpos($filename, ".amr")== False)
	{
		exit();
	}
	$updir = 'upload/';
	$savefile = $updir.$filename;
	$hd1 = fopen($savefile,'wb+'); 
	$hd2 = fopen('php://input','rb');
	while (!feof($hd2))
	{
	    fwrite($hd1, fread($hd2, 1024));
	}
	fclose($hd1);
	fclose($hd2);
}
else if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
	$spid = $_GET["spid"];
	$filename = explode('/', $_SERVER["REQUEST_URI"])[4];
	$url = $Host[$spid]["sound"];
	Logs("------get-----");
	Logs($spid);
	Logs($filename);
	Logs($url);
	header('location:'.$url.'/sound/upload/'.$filename);
}
exit();